package com.openbravo.data.gui.muleSelecter;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JScrollPane;
import javax.swing.JList;

public class JMuleObjectSelecter extends JPanel {

    List<MuleComboBoxValEntry> entrys;
    JList list;
    
	/**
	 * Create the panel.
	 */
	public JMuleObjectSelecter() {
		setLayout(new BorderLayout(0, 0));
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);
		
		list = new JList();
		scrollPane.setViewportView(list);
		list.addMouseListener(new MouseAdapter(){

			@Override
			public void mouseClicked(MouseEvent e) {
				int selectIndex=list.getSelectedIndex();
				if(selectIndex>=0){
					MuleComboBoxValEntry entry=entrys.get(selectIndex);
					entry.setSelected(!entry.isSelected());
				}
			}
		});
		
		list.setCellRenderer(new MuleComboxBoxListCellRenderer());
	}
	
  public List<MuleComboBoxValEntry> getEntrys() {
		return entrys;
	}

	public void setEntrys(List<MuleComboBoxValEntry> entrys) {
		this.entrys = entrys;
		list.setModel(new MyListData(entrys));
	}




private static class MyListData extends javax.swing.AbstractListModel {
        
	  List<MuleComboBoxValEntry> m_data;
        
        public MyListData(List data) {
            m_data = data;
        }
        
        public Object getElementAt(int index) {
            return m_data.get(index);
        }
        
        public int getSize() {
            return m_data.size();
        } 
    }
	
	public List<MuleComboBoxValEntry> getSelectedList(){
		ArrayList<MuleComboBoxValEntry> retList=new ArrayList<MuleComboBoxValEntry>();
		for(MuleComboBoxValEntry e:entrys)
		   if(e.isSelected())
			   retList.add(e);
		return retList;
	}

}
